package com.yd.mvc.userController.user;

import com.yd.app.service.mobile.couponService.MCouponService;
import com.yd.app.service.mobile.userService.MUserService;
import com.yd.app.service.pc.roleService.RoleService;
import com.yd.app.service.sys.UserService;
import com.yd.app.util.BusinessException;
import com.yd.domain.business.UserCoupon;
import com.yd.domain.commom.PageModel;
import com.yd.domain.searchParam.UserSearchParam;
import com.yd.domain.sys.Perm;
import com.yd.domain.sys.Role;
import com.yd.domain.sys.User;
import com.yd.domain.sys.UserRole;
import com.yd.mvc.baseController.util.AuthUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;


@RestController
public class UserController {

	@Autowired
	UserService userService;
	@Autowired
	MUserService muserService;
	@Autowired
	RoleService roleService;
	@Autowired
	MCouponService mCouponService;

	/**
	 * 用户信息注册
	 * @param user
	 * @return
	 * @throws Exception
     */
	@RequestMapping(value = "/dapi/user/register", method = RequestMethod.POST)
	public String userRegister(@RequestBody User user)throws Exception {
		String registCode="";
		if(user!=null){
			registCode=userService.userRegister(user);
		}
		return registCode;
	}
	/**
	 * 通过id删除用户信息
	 * @param userIds
	 * @throws Exception
     */
	@RequestMapping(value = "/api/user/delete", method = RequestMethod.POST)
	public void userDelete(@RequestBody String [] userIds)throws Exception {
		for(String userId:userIds){
			userService.deleteUserById(userId);
		}
	}
	/**
	 * 修改用户信息
	 * @param user
	 * @throws Exception
     */
	@RequestMapping(value = "/dapi/user/update", method = RequestMethod.POST)
	public void userUpdate(@RequestBody User user)throws Exception {
		if(user!=null){
			userService.userUpdate(user);
		}
	}
	/**
	 * 用户信息列表
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value = "/api/user/list/{pageIndex}/{pageSize}", method = RequestMethod.POST)
	public PageModel userList(@RequestBody UserSearchParam param, @PathVariable("pageIndex")int pageIndex, @PathVariable("pageSize")int pageSize)throws Exception {
		AuthUtil.AuthedUser user = AuthUtil.getCurrentUser();
		PageModel pm=null;
		if(user!=null){
			pm=userService.userList(param,pageIndex,pageSize,user.getUserId());
		}else{
			throw new BusinessException(401, "未登录");
		}
		return pm;
	}
	/**
	 * 用户信息列表
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value = "/api/user/listwithMsg/{pageIndex}/{pageSize}", method = RequestMethod.POST)
	public PageModel userListWithMsg(@RequestBody UserSearchParam param, @PathVariable("pageIndex")int pageIndex, @PathVariable("pageSize")int pageSize)throws Exception {
		AuthUtil.AuthedUser user = AuthUtil.getCurrentUser();
		PageModel pm=null;
		if(user!=null){
			pm=userService.userListWithMsg(param,pageIndex,pageSize,user.getUserId());
		}else{
			throw new BusinessException(401, "未登录");
		}
		return pm;
	}
	/**
	 * 通过用户id查询用户信息
	 * @param userId
	 * @return
	 * @throws Exception
     */
	@RequestMapping(value = "/api/user/detail/{userId}", method = RequestMethod.GET)
	public User userDetail(@PathVariable("userId")String userId)throws Exception {
		User user=userService.getUserById(userId);
		return user;
	}
	/**
	 * 通过用户id查询用户信息
	 * @param userId
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/api/user/getRole/{userId}", method = RequestMethod.GET)
	public String getRole(@PathVariable("userId")String userId)throws Exception {
		String roles=userService.getRole(userId);
		return roles;
	}
	/**
	 * 审核用户
	 * @param user
	 * @return
	 * @throws Exception
     */
	@RequestMapping(value = "/api/user/checkUser", method = RequestMethod.POST)
	public void userCheckByuserId(@RequestBody User user)throws Exception {
		userService.userCheckByuserId(user);
	}
	/**
	 *通过用户id查询用户角色信息
	 * @param userId
	 * @return
	 * @throws Exception
     */
	@RequestMapping(value = "/api/user/role/{userId}", method = RequestMethod.GET)
	public List<Role> getUserRole(@PathVariable("userId")String userId)throws Exception {
		List<Role>list=roleService.getUserRole(userId);
		return list;
	}
	/**
	 *通过用户id查询用户权限信息
	 * @param userId
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/api/user/perm/{userId}", method = RequestMethod.GET)
	public List<Perm> getUserPerm(@PathVariable("userId")String userId)throws Exception {
		List<Perm>list=roleService.getUserPerm(userId);
		return list;
	}

	/**
	 * 删除或者添加用户角色
	 * @param roleId
	 * @throws Exception
     */
	@RequestMapping(value = "/dapi/role/{userId}/{roleId}/{flag}", method = RequestMethod.GET)
	public void saveUserRole(@PathVariable("userId")String userId,@PathVariable("roleId")String roleId,@PathVariable("flag")boolean flag)throws Exception {
		if(flag){
			//添加角色
			roleService.saveUserRole(new UserRole(userId,roleId));
		}else{
			//删除角色
			roleService.deleteUserRole(new UserRole(userId,roleId));
		}
	}
	//修改密码
	@RequestMapping(value = "/api/user/updatePwd/{oldPwd}/{newPwd}", method = RequestMethod.GET)
	public void updateUserPwd(@PathVariable("oldPwd")String oldPwd,@PathVariable("newPwd")String newPwd, HttpSession session) throws Exception {
		AuthUtil.AuthedUser user=AuthUtil.getCurrentUser();
		//数据获取成功---更新已读状态
		if(user!=null){
			muserService.updateUserPwd(user.getUserId(),oldPwd,newPwd);
		}else{
			throw new BusinessException(401,"尚未登录","no-login");
		}
	}

	/**
	 * 我的优惠券
	 * @param session
	 * @return
	 * @throws BusinessException
     */
	@RequestMapping(value = "/api/userCoupon//getUserCouponList", method = RequestMethod.GET)
	public List<UserCoupon> getCouponList(HttpSession session) throws BusinessException {
		AuthUtil.AuthedUser user=AuthUtil.getCurrentUser();
		List<UserCoupon> list = null;
		try {
			if(user!=null){
				list = mCouponService.getCouponList(user.getUserId());
				for(int i =0;i<list.size();i++){
					if (list.get(i).getCoupon()==null) {
						list.remove(i);
					}
				}
			}else{
				throw new BusinessException(500, "尚未登录","no-login");
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new BusinessException(500, "获取用户优惠券信息失败");
		}
		return list;
	}


	/**
	 * 验证存在用户名手机号
	 *
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/dapi/muser/justPhoneAndName/{username}/{phone}", method = RequestMethod.GET)
	public Integer justPhoneAndName(@PathVariable("username")String username,@PathVariable("phone")String phone)throws Exception {
		List<User> userList=userService.justPhoneAndName(username,phone);
		return userList.size();
	}


	/**
	 * 修改密码
	 *
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/dapi/muser/changePwd/{username}/{phone}/{password}", method = RequestMethod.GET)
	public Integer changePwd(@PathVariable("username")String username,@PathVariable("phone")String phone,@PathVariable("password")String password)throws Exception {
		int count=userService.changePwd(username,phone,password);
		return count;
	}
}
